﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using anaii.EasyNews.Data;
using System.Collections;
using System.Threading;
using anaii.EasyNews.Controller;

namespace anaii.EasyNews
{
    public partial class View : PortalModuleBase,IActionable
    {
        #region Variables

        private int NewsIDFromQS
        {
            get 
            {
                int retVal = -1;

                string QueryStringName = "NewsID";
                ModuleController controller = new ModuleController();
                Hashtable TabModuleSettingsCurrent = controller.GetTabModuleSettings(this.TabModuleId);
                if (!string.IsNullOrEmpty((string)TabModuleSettingsCurrent["NewsItemQueryStringKey"]))
                {
                    if (!string.IsNullOrEmpty((string)TabModuleSettingsCurrent["NewsItemQueryStringKey"]))
                        QueryStringName = (string)TabModuleSettingsCurrent["NewsItemQueryStringKey"];
                }

                if (!string.IsNullOrEmpty(Request.QueryString[QueryStringName]) && int.TryParse(Request.QueryString[QueryStringName], out retVal))
                {
                    return retVal;
                }
                return -1;
            }
        }

        #endregion

        #region Base

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #endregion

        #region Private methods

        private void BindData()
        {
            EasyNewsDBDataContext db = new EasyNewsDBDataContext();
            EasyNew currentNews = new EasyNew();
            
            ModuleController controller = new ModuleController();
            Hashtable TabModuleSettingsCurrent = controller.GetTabModuleSettings(this.TabModuleId);

            int NewsType = 0;
            int NewsStatus = 1;
            bool ShowDefaultItem = true;
            if (!string.IsNullOrEmpty((string)TabModuleSettingsCurrent["NewsItemSelectedNewsType"]))
            {
                if (!int.TryParse(TabModuleSettingsCurrent["NewsItemSelectedNewsType"].ToString(), out NewsType))
                    NewsType = 0;
            }
            if (!string.IsNullOrEmpty((string)TabModuleSettingsCurrent["NewsItemSelectedNewsStatus"]))
            {
                if (!int.TryParse(TabModuleSettingsCurrent["NewsItemSelectedNewsStatus"].ToString(), out NewsStatus))
                    NewsStatus = 1;
            }
            if (!string.IsNullOrEmpty((string)TabModuleSettingsCurrent["NewsItemShowDefaultItem"]))
            {
                if (!bool.TryParse(TabModuleSettingsCurrent["NewsItemShowDefaultItem"].ToString(), out ShowDefaultItem))
                    ShowDefaultItem = true;
            }

            if (NewsIDFromQS > 0)
            {
                currentNews = db.EasyNews.FirstOrDefault(p => p.NewsID == NewsIDFromQS);
            }
            else if (UserInfo.UserID >= 1 && UserInfo.UserID <= 2) //Show news for Super User and Admin
            {
                if (NewsStatus == 1 && NewsType == 0)
                    currentNews = db.EasyNews.OrderByDescending(p => p.DateCreated).FirstOrDefault(p => p.IsPublished == true);
                if (NewsStatus == 2 && NewsType == 0)
                    currentNews = db.EasyNews.OrderByDescending(p => p.DateCreated).FirstOrDefault(p => p.IsArchived == true);
                if (NewsStatus == 1 && NewsType != 0)
                    currentNews = db.EasyNews.OrderByDescending(p => p.DateCreated).FirstOrDefault(p => p.IsPublished == true && p.TypeID == NewsType);
                if (NewsStatus == 2 && NewsType != 0)
                    currentNews = db.EasyNews.OrderByDescending(p => p.DateCreated).FirstOrDefault(p => p.IsArchived == true && p.TypeID == NewsType);
            }
            else //Show news for all other users and not logged in users
            {
                if (NewsStatus == 1 && NewsType == 0)
                    currentNews = db.EasyNews.OrderByDescending(p => p.DateCreated).FirstOrDefault(p => p.IsPublished == true && (!p.DatePublished.HasValue || p.DatePublished <= DateTime.Now));
                if (NewsStatus == 2 && NewsType == 0)
                    currentNews = db.EasyNews.OrderByDescending(p => p.DateCreated).FirstOrDefault(p => p.IsArchived == true);
                if (NewsStatus == 1 && NewsType != 0)
                    currentNews = db.EasyNews.OrderByDescending(p => p.DateCreated).FirstOrDefault(p => p.IsPublished == true && p.TypeID == NewsType && (!p.DatePublished.HasValue || p.DatePublished <= DateTime.Now));
                if (NewsStatus == 2 && NewsType != 0)
                    currentNews = db.EasyNews.OrderByDescending(p => p.DateCreated).FirstOrDefault(p => p.IsArchived == true && p.TypeID == NewsType);
            }

            if (currentNews == null)
                return;

            bool add = true;
            if (!ShowDefaultItem)
                add = false;
            if (NewsStatus == 1 && !currentNews.IsPublished)
                add = false;
            if (NewsStatus == 2 && !currentNews.IsArchived)
                add = false;
            if (NewsType != 0 && currentNews.TypeID != NewsType)
                add = false;
            if (!currentNews.IsEnabled)
                add = false;
            if (currentNews.ExpireDate.HasValue && DateTime.Compare(DateTime.Now, currentNews.ExpireDate.Value) >= 0)
                add = false;

            if (add)
            {
                string template = (string)TabModuleSettingsCurrent["NewsTemplate" + Thread.CurrentThread.CurrentCulture.Name];
                template = EasyNewsController.ReplaceTokens(template, currentNews);
                litOutput.Text = Server.HtmlDecode(template);
            }
        }

        #endregion

        #region IActionable Members

        public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions
        {
            get
            {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), "Edit News", ModuleActionType.AddContent, "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion
    }
}